package models

import (
	"github.com/astaxie/beego/orm"
	"strconv"
	"strings"
)

type GroupMenuModels struct {
}

type GroupMenu struct {
	Id       int64
	Group_id int64
	Menu_id  int64
}

func init() {
	orm.RegisterModel(new(GroupMenu))
}

//获取
func (g *GroupMenuModels) Query(gid int64) ([]GroupMenu, error) {
	var list []GroupMenu
	ps := getQueryTable("group_menu")
	_, err := ps.Filter("group_id", gid).All(&list)
	return list, err
}

//修改
func (g *GroupMenuModels) Update(maps map[string]interface{}) (int64, error) {
	gid, _ := strconv.ParseInt(maps["group_id"].(string), 10, 64)
	gm := GroupMenu{Group_id: gid}
	menuids := strings.Split(maps["menu_id"].(string), ",")
	o := orm.NewOrm()
	_, err := o.Delete(&gm, "Group_id")
	var row int64
	if err == nil {
		//删除成功，重新插入
		for i := 0; i < len(menuids); i++ {
			row, _ = strconv.ParseInt(menuids[i], 10, 64)
			row, err = o.Insert(&GroupMenu{Group_id: gid, Menu_id: row})
		}
	}
	return row, err
}
